home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
8206
/
8206.xpi
/
content
/
comm.js
< prev
next >
Wrap
Text File
|
2010-02-02
|
9KB
|
332 lines
var WiseStampComm = function() {
var pub = {};
pub.RESULT_OK = 0;
pub.RESULT_FAILURE = 1;
pub.RESULT_SERVER_DOWN = 2;
//pub.HOST = "http://local.wisestamp";
pub.HOST = "http://my.wisestamp.com";
pub.LOG_EVENT_INIT = "addon_init";
pub.LOG_EVENT_SETTINGS = "addon_settings";
pub.LOG_EVENT_SIG_INSERT = "addon_sig_insert";
//pub.LOG_EVENT_INIT = "addon_init";
var URL_WRITE_LOG = "/xml_api/write_log";
var URL_LOGIN = "/xml_api/login";
var URL_REGISTER = "/xml_api/register";
var URL_SAVE_SIGNATURES = "/xml_api/save_signatures";
var URL_LOAD_SIGNATURE = "/xml_api/load_signature";
var URL_LOAD_SIGNATURES = "/xml_api/load_signatures";
var URL_LOAD_SETTINGS = "/xml_api/load_settings";
pub.xmlHttpObjects = new Array();
pub.mSettings = null;
///////////////////////////////////////////////////////////////////////////
pub.init = function()
{
var server_hostname = WiseStampPrefs.getCharPref("server");
if (server_hostname != "")
pub.HOST = server_hostname;
WiseStampUtils.log("comm.js :: init :: server hostname = " + pub.HOST);
}
function xmlToObject(xml)
{
var object = {};
//WiseStampUtils.log("[comm.js::xmlToObject] HasChildNodes = " + xml.hasChildNodes());
object.nodeCount = xml.childNodes.length;
if (xml.hasChildNodes())
{
//WiseStampUtils.log("[comm.js::xmlToObject] childNodes.length = " + xml.childNodes.length);
for (var i = 0; i < xml.childNodes.length; i++)
{
var child = xml.childNodes[i];
//WiseStampUtils.log("[comm.js::xmlToObject] node " + i + " :: type = " + child.nodeType + ", tag = " + child.tagName + ", value = <" + child.nodeValue + ">");
switch (child.nodeType)
{
case 1: // Element Node
if (object.nodes == undefined)
object.nodes = {};
object.nodes[child.tagName] = xmlToObject(child);
break;
case 2: // Attribute Node
if (object.attributes == undefined)
object.attributes = {};
object.attributes[child.tagName] = child.nodeValue;
break;
case 3: // Text Node
case 4: // CDATA Section Node
object.text = child.nodeValue;
break;
/*
case 5: // Entity Reference Node
case 6: // Entity Node
case 7: // Processing Instruction Node
case 8: // Comment Node
case 9: // Document Node
case 10: // Document Type Node
case 11: // Document Fragment Node
case 12: // Notation Node
*/
}
}
}
//WiseStampUtils.log("[comm.js::xmlToObject] <<<<");
return object;
}
function serializeParams(params)
{
var str = "";
for (key in params)
{
if (str.length > 0)
str += "&";
str += key + '=' + params[key];
}
return str;
}
///////////////////////////////////////////////////////////////////////////
function createXmlHttpObjectIndex()
{
var i = Math.random()*100;
i = Math.round(i);
WiseStampUtils.log("[comm.js::acquireXmlHttpObjectIndex] Created object at index " + i);
WiseStampComm.xmlHttpObjects[i] = {};
WiseStampComm.xmlHttpObjects[i].isFree = false;
return i;
}
function getXmlHttpObject(index)
{
//WiseStampUtils.log("[comm.js::getXmlHttpObject] xmlHttpObjects = " + WiseStampComm.xmlHttpObjects);
//WiseStampUtils.log("[comm.js::getXmlHttpObject] index = " + index + ", xmlHttpObjects.length = " + WiseStampComm.xmlHttpObjects.length);
//WiseStampUtils.log("[comm.js::getXmlHttpObject] xmlHttpObjects["+index+"] = " + WiseStampComm.xmlHttpObjects[index]);
return WiseStampComm.xmlHttpObjects[index];
}
function releaseXmlHttpObject(index)
{
xmlHttpObjects[i] = null;
}
pub.sendRequest = function(url,params,async,callback)
{
WiseStampUtils.log("comm.js :: sendRequest :: url = " + url);
try {
if (params.password != undefined)
{
if (params.password.length != 32)
params.password = WiseStampUtils.md5(params.password);
}
var iXmlHttp = createXmlHttpObjectIndex();
getXmlHttpObject(iXmlHttp).xmlhttp = new XMLHttpRequest();
getXmlHttpObject(iXmlHttp).callback = callback;
getXmlHttpObject(iXmlHttp).xmlhttp.open("POST",this.HOST+url,async);
getXmlHttpObject(iXmlHttp).xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
getXmlHttpObject(iXmlHttp).xmlhttp.send(serializeParams(params));
if (async == true)
{
getXmlHttpObject(iXmlHttp).xmlhttp.onreadystatechange = function() {WiseStampComm.onSendResponse(iXmlHttp);};
}
else
{
return this.onSendResponse(iXmlHttp);
}
}
catch (e)
{
WiseStampUtils.log("[comm.js::sendRequest] url = " + url + ", error = " + e);
return this.LOGIN_RESULT_SERVER_DOWN;
}
return this.RESULT_OK;
}
pub.onSendResponse = function(iXmlHttp)
{
var xmlhttp = getXmlHttpObject(iXmlHttp).xmlhttp;
var callback = getXmlHttpObject(iXmlHttp).callback;
WiseStampUtils.log("[comm.js::onSendResponse] xmlhttp.readyState = " + xmlhttp.readyState + ", xmlhttp.status = " + xmlhttp.status);
if (xmlhttp.readyState != 4)
return null;
if (xmlhttp.status != 200)
return null;
WiseStampUtils.log("[comm.js::onSendResponse] XML = " + xmlhttp.responseText);
// if "OK"
try {
var xml = null;
if (xmlhttp.responseXML != undefined)
xml = xmlToObject(xmlhttp.responseXML.firstChild);
if (xml == null || xml.nodes.result == undefined)
{
xml = null;
WiseStampUtils.log("<<< WiseStampComm.onSendResponse. Problem with server. XML = " + xmlhttp.responseText);
}
if (callback != null)
{
//WiseStampUtils.log("[comm.js::onSendResponse] calling callback..." + callback);
callback(xml);
}
//releaseXmlHttpObject(iXmlHttp);
return xml;
}
catch (e)
{
WiseStampUtils.log("[comm.js::onSendResponse] " + e);
}
return null;
}
///////////////////////////////////////////////////////////////////////////
pub.writeLogResponse = function(xml)
{
if (xml.nodes.result == "0")
WiseStampUtils.log("WiseStampComm.writeLogResponse. result = " + xml.nodes.result);
}
pub.writeLog = function(event,param1,param2,param3,param4,param5)
{
if (WiseStampComm.loadSettings('collect_stats') != 1)
return;
param1 = param1 != null ? param1 : "";
param2 = param2 != null ? param2 : "";
param3 = param3 != null ? param3 : "";
param4 = param4 != null ? param4 : "";
param5 = param5 != null ? param5 : "";
var params = {event:event, user_id:WiseStampUtils.getUserCode(), param1:param1, param2:param2, param3:param3, param4:param4, param5:param5};
this.sendRequest(URL_WRITE_LOG,params,true,pub.writeLogResponse);
}
///////////////////////////////////////////////////////////////////////////
pub.login = function(email,password,callback,async)
{
var params = {email:email, password:password};
return this.sendRequest(URL_LOGIN,params,async,callback);
}
pub.register = function(email,password,firstname,lastname)
{
var params = {email:email, password:password, firstname:firstname, lastname:lastname};
var xml = this.sendRequest(URL_REGISTER,params,false);
return xml;
}
///////////////////////////////////////////////////////////////////////////
pub.saveSignatures = function(email,password,signatures)
{
var params = {email: email, password: password};
for (var i = 0; i < signatures.length; i++)
{
params['signature_'+i+'_type'] = signatures[i].type;
params['signature_'+i+'_data'] = signatures[i].data;
}
var xml = this.sendRequest(URL_SAVE_SIGNATURES,params,false);
return xml;
}
pub.loadSignatures = function(email,password,type)
{
var params = {email: email, password: password};
var xml = this.sendRequest(URL_LOAD_SIGNATURES,params,false);
return xml;
}
pub.loadSignature = function(email,password,type)
{
var params = {email: email, password: password, type: type};
var xml = this.sendRequest(URL_LOAD_SIGNATURE,params,false);
return xml;
}
pub.loadSettings = function(parameter)
{
return false;
if (this.mSettings == null)
{
WiseStampUtils.log("[comm.js::loadSettings] loading from server...");
var params = {};
var xml = this.sendRequest(URL_LOAD_SETTINGS,params,false);
WiseStampUtils.log("[comm.js::loadSettings] received xml = " + xml);
if (xml != null)
{
var settings = xml.nodes.data.nodes;
for (var item in settings)
settings[item] = settings[item].text;
this.mSettings = settings;
}
if (this.mSettings == null)
{
WiseStampUtils.log("[comm.js::loadSettings] failed to load settings");
this.mSettings = false; // change value to 'false' to indicate a failure
} else
WiseStampUtils.log("[comm.js::loadSettings] settings loaded succesfully - " + this.mSettings.toSource());
}
if (this.mSettings == false)
return false;
return this.mSettings[parameter];
}
///////////////////////////////////////////////////////////////////////////
return pub;
}();
WiseStampComm.init();